package at.jku.se.fastsell.data;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class Auction {
	
	private long auctionID;
	
	private String title, description;
	private double curPrice;
	private User seller;
	private Date start, end;
	private List<Bid> bids;
	private int bidCount;
	
	public Auction() {
		
	}
	
	// ----------------------------------------------------------------------------------
	
	private Auction(long auctionID, String title, String description, User seller, double startingPrice) {
		this.auctionID = auctionID;
		this.title = title;
		this.description = description;
		this.curPrice = startingPrice;
		this.seller = seller;
		this.bids = new LinkedList<Bid>();
	}
	
	public void setAuctionID(long auctionID) {
		this.auctionID = auctionID;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setCurPrice(double curPrice) {
		this.curPrice = curPrice;
	}

	public void setSeller(User seller) {
		this.seller = seller;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public void setBids(List<Bid> bids) {
		this.bids = bids;
	}

	public void setBidCount(int bidCount) {
		this.bidCount = bidCount;
	}

	public Auction(long auctionID, String title, String description, User seller, double startingPrice, Date start, Date end) {
		this(auctionID, title, description, seller, startingPrice);
		
		this.start = start;
		this.end = end;
	}
	
	public Auction(long auctionID, String title, String description, User seller, double startingPrice, int durationInDays) {
		this(auctionID, title, description, seller, startingPrice);
		
		this.start = new Date();
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(start);
		cal.add(Calendar.DATE, durationInDays);
		this.end = cal.getTime();
	}
	
	// ----------------------------------------------------------------------------------
	
	protected boolean addBid(User bidder, double price) {
		if (price > curPrice && isActive()) {
			curPrice = price;
			bids.add(new Bid(bidder, price));
			bidCount++;
			return true;
		}
		return false;
	}
	
	public boolean isActive() {
		if (new Date().after(end)) {
			return false;
		}
		return true;
	}
	
	public String getRemainingTime() {
		if (isActive()) {
			long diff = end.getTime() - new Date().getTime();
			
			long diffSeconds = diff / 1000 % 60;
			long diffMinutes = diff / (60 * 1000) % 60;
			long diffHours = diff / (60 * 60 * 1000) % 24;
			long diffDays = diff / (24 * 60 * 60 * 1000);
			
			return (diffDays + "d " + diffHours + "h " + diffMinutes + "m " + diffSeconds + "s");
		}
		return "ended";
	}
	
	public User getHighestBidder() {
		if (!bids.isEmpty()) {
			User bidder = bids.get(0).getBidder();
			double bidPrice = bids.get(0).getPrice();
			
			for (Bid b : bids) {
				if (b.getPrice() > bidPrice) {
					bidPrice = b.getPrice();
					bidder = b.getBidder();
				}
			}
			return bidder;
		}
		return null;
	}
	
	public boolean hasBids() {
		if (bidCount > 0) {
			return true;
		}
		return false;
	}
	
	// ----------------------------------------------------------------------------------
	
	public long getAuctionID() {
		return auctionID;
	}
	public String getTitle() {
		return title;
	}
	public String getDescription() {
		return description;
	}
	public User getSeller() {
		return seller;
	}
	public double getCurPrice() {
		return curPrice;
	}
	public Date getStart() {
		return start;
	}
	public Date getEnd() {
		return end;
	}
	public List<Bid> getBids() {
		return bids;
	}
	public int getBidCount() {
		return bidCount;
	}


}
